package com.blog.exception;

import com.blog.common.constant.ResponseCode;
import com.blog.common.ServiceResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * describe:
 *
 * @author vv
 * @date 2018/12/04/23:02
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(value = MyException.class)
    @ResponseBody

    /**
     *describe:
     *@author vv
     *@date 2018/12/4 23:06
     */
    public ServiceResponse jsonErrorHandler(HttpServletRequest req, MyException e) {
        log.error("{}", e.getMessage());
        return ServiceResponse.createByErrorMessage(e.getMessage());
    }

    /**
     * describe:
     *
     * @author vv
     * @date 2018/12/4 23:03
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ServiceResponse defaultErrorHandler(Exception e) {
        log.error("{}", e.getMessage());
        return ServiceResponse.createByError(ResponseCode.UNKNOW_ERROR);
    }


}
